<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Admin\Model\LandModel;

class LandController extends AuthController {

    protected $limit = 10;

    //农作物
    public function crops_list() {
//        $lands=D('CropsInfo');
//        $data=$lands->selectCrops();
        $count = M('crops_info')->count();
        $Page = new \Think\Page($count, $this->limit);
        $show = $Page->show(); // 分页显示输出
        $crops = M('crops_info')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('crops', $crops);
        $this->assign('page', $show);
        $this->display();
    }

    public function crops_add() {
        if (!IS_POST) {
            $this->display();
        } else {
            $data = array(
                'crops_name' => I('crops_name'),
                'crops_seed' => I('crops_seed'),
                'crops_sprout' => I('crops_sprout'),
                'crops_maturity' => I('crops_maturity'),
                'crops_sprout_img' => I('uploader_file'),
                'crops_grow_img' => I('uploader_file1'),
                'crops_ripe_img' => I('uploader_file2'),
                'display' => I('display'),
                'add_time' => time(),
            );
            M('crops_info')->add($data);
            $this->success('农作物保存成功', U('crops_list'), 1);
        }
    }

    public function crops_edit() {
        if (!IS_POST) {
            $id = I('id');
            $crops = M('crops_info');
            $crops = $crops->where(array('id' => $id))->find();
            $this->assign('crops', $crops);
            $this->display();
        } else {
            $data = array(
                'crops_name' => I('crops_name'),
                'crops_seed' => I('crops_seed'),
                'crops_sprout' => I('crops_sprout'),
                'crops_maturity' => I('crops_maturity'),
                'crops_sprout_img' => I('uploader_file'),
                'crops_grow_img' => I('uploader_file1'),
                'crops_ripe_img' => I('uploader_file2'),
                'display' => I('display')
            );
            M('crops_info')->where('id=' . I('edit_id'))->save($data);
            $this->success('农作物修改成功', U('crops_list'), 1);
        }
    }

    //删除农作物
    public function crops_del() {
        $column = M('crops_info')->where(array('id' => I('del_id')))->delete();
        $this->redirect('crops_list');
    }

    //土地类型管理
    public function land_class_list() {
        $count = M('land_info')->count();
        $Page = new \Think\Page($count, $this->limit);
        $show = $Page->show(); // 分页显示输出
        $lands = M('land_info')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($lands as $key=>$val) {
            //转化为农作物名称
            if (!empty($val['landclass_crops'])) {
                $landclass_crops=makeSemiangle($val['landclass_crops']);
                $crops = M('crops_info')->where(array('id' => array('in',$landclass_crops)))->select();
                if (count($crops) > 0) {
                    $lands[$key]['landclass_crops']='';
                    foreach ($crops as $v) {
                        $lands[$key]['landclass_crops'].=$v['crops_name'].' ';
                    }
                }
            }
        }
        $this->assign('lands', $lands);
        $this->assign('page', $show);
        $this->display();
    }

    //土地类型增加
    public function land_class_add() {
        if (!IS_POST) {
            $this->display();
        } else {
            $data = array(
                'landclass_name' => I('landclass_name'),
                'landclass_upgrade' => I('landclass_upgrade'),
                'landclass_crops' => I('landclass_crops'),
                'landclass_img' => I('uploader_file'),
                'display' => I('display'),
                'add_time' => time(),
            );
            M('land_info')->add($data);
            $this->success('土地类型保存成功', U('land_class_list'), 1);
        }
    }

    //土地类型修改
    public function land_class_edit() {
        if (!IS_POST) {
            $id = I('id');
            $crops = M('land_info')->where(array('id' => $id))->find();
            $this->assign('lands', $crops);
            $this->display();
        } else {
            $data = array(
                'landclass_name' => I('landclass_name'),
                'landclass_upgrade' => I('landclass_upgrade'),
                'landclass_crops' => I('landclass_crops'),
                'landclass_img' => I('uploader_file'),
                'display' => I('display')
            );
            M('land_info')->where('id=' . I('edit_id'))->save($data);
            $this->success('地块类型修改成功', U('land_class_list'), 1);
        }
    }

    //删除土地类型
    public function land_class_del() {
        $column = M('land_info')->where(array('id' => I('del_id')))->delete();
        $this->redirect('land_class_list');
    }

    public function upimage() {
        return ajaxUploader();
    }

    

}
